package com.timebank.volunteer.service.impl;

import com.timebank.common.core.constant.SecurityConstants;
import com.timebank.common.core.constant.VolunteerExceptionConstant;
import com.timebank.common.core.exception.volunteer.VolunteerException;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.common.security.utils.SecurityUtils;
import com.timebank.timecoin.service.RemoteTimeCoinService;
import com.timebank.volunteer.mapper.VolunteerAttendListMapper;
import com.timebank.volunteer.mapper.VolunteerBlackListMapper;
import com.timebank.volunteer.mapper.VolunteerListMapper;
import com.timebank.volunteer.mapper.VolunteerMapper;
import com.timebank.volunteer.pojo.entity.Volunteer;
import com.timebank.volunteer.pojo.entity.VolunteerAttendList;
import com.timebank.volunteer.pojo.entity.VolunteerList;
import com.timebank.volunteer.pojo.vo.VolunteerAttendListVO;
import com.timebank.volunteer.pojo.vo.VolunteerListVO;
import com.timebank.volunteer.service.VolunteerBlackListService;
import com.timebank.volunteer.service.VolunteerListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shiliuyinzhen
 */
@Service
public class VolunteerListServiceImpl implements VolunteerListService {

    @Resource
    private VolunteerListMapper volunteerListMapper;

    @Resource
    private VolunteerMapper volunteerMapper;

    @Resource
    private VolunteerBlackListService volunteerBlackListService;

    @Resource
    private VolunteerAttendListMapper volunteerAttendListMapper;

    /**
     * 获取志愿服务参与名单列表
     * @param volunteerId
     * @return
     */
    @Override
    public List<VolunteerListVO> getVolunteerListById(Long volunteerId) {
        return volunteerListMapper.getVolunteerListById(volunteerId);
    }

    /**
     * 批量删除志愿服务参与者
     * @param userIds
     * @param volunteerId
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deleteVolunteerListByUserIds(Long[] userIds, Long volunteerId) {
        Long userId = SecurityUtils.getUserId();
        //如果不是管理员
        if (!SecurityUtils.isAdmin(userId)){
            Volunteer volunteer = volunteerMapper.getVolunteerById(volunteerId);
            //如果发布者不是该用户
            if (volunteer.getPublisherId() != userId){
                throw new VolunteerException(VolunteerExceptionConstant.USER_NOT_HAVE_PERMISSION);
            }
        }
        //批量删除志愿服务参与者
        volunteerListMapper.deleteVolunteerListByUserIds(userIds,volunteerId);
        //将删除的志愿者拉入黑名单
        volunteerBlackListService.insertBlackListByUserIdsAndVolunteerId(userIds,volunteerId);
        //查询志愿服务信息
        Volunteer volunteer = volunteerMapper.getVolunteerById(volunteerId);
        //更新志愿服务信息
        volunteer.setCurrentVolunteerNum(volunteer.getCurrentVolunteerNum() - userIds.length);
        return volunteerMapper.updateVolunteerById(volunteer) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 获取缺席名单
     * @param volunteerId
     * @return
     */
    @Override
    public List<VolunteerListVO> getAbsentList(Long volunteerId) {
        //查询出席名单
        VolunteerAttendList volunteerAttendList = VolunteerAttendList.builder()
                .volunteerId(volunteerId)
                .build();
        List<VolunteerAttendListVO> attendLists =
                volunteerAttendListMapper.getAttendList(volunteerAttendList);
        if (attendLists == null || attendLists.size() == 0){
            return volunteerListMapper.getVolunteerListById(volunteerId);
        }
        List<Long> userIds = new ArrayList<>();
        for (VolunteerAttendListVO attendList : attendLists) {
            Long userId = attendList.getUserId();
            userIds.add(userId);
        }
        //查询缺席名单
        List<VolunteerListVO> list = volunteerListMapper.getAbsentListById(volunteerId,userIds);
        return list;
    }
}
